

//
//  TheLoginViewController.h
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.

#import "TheLoginViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIViewExt.h"
#import <objc/runtime.h>
#import <RongIMKit/RongIMKit.h>
#import "TheLoginView.h"
#import "ForgotPasswordViewController.h"
#import "RegistedViewController.h"
#import "ToBindCompanyViewController.h"
@interface TheLoginViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate ,RCIMUserInfoDataSource,TheLoginViewDelegate,RCIMConnectionStatusDelegate,
RCIMReceiveMessageDelegate>
@property (retain,nonatomic)UIImageView *recordBtn;
@end

@implementation TheLoginViewController

{
      MBProgressHUD *progressHud;
}
@synthesize alt = alt;
@synthesize mDelegate;
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
    }

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [[RCIMClient sharedRCIMClient] disconnect:NO];
    
    [Global setRongYunToken:@""];

    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@""];
    CGRect backframe = CGRectMake(0,0,23/2,41/2);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
}
-(void)viewDidLoad{
    
    
    [super viewDidLoad];
    
    
    self.title = @"用户登录";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];

    
    [self initView];
       
    
    
}

- (void)initView
{
    TheLoginView *container = [[TheLoginView alloc]initWithFrame:self.view.bounds];
    container.mDelegate=self;
    [self.view addSubview:container];
    }

#pragma mark Event Handle

-(void)userName:(NSString *)userName pwd:(NSString *)password{

    if ([userName length] <1)
    {
        [self showAlertMessage:@"请输入账号"];
    }
    else{
     if ([password length] < 1)
        {
            [self showAlertMessage:@"请输入密码"];
        }
        else
        {
            [Global showIndeterminateHud:@"登录中，请稍后..." delay:30 view:self.view];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* jpushRegId = [user objectForKey:@"jpushRegId"];

            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:userName forKey:@"mobile"];
            [params setValue:[MyMD5 md5:password] forKey:@"loginWord"];
            [params setValue:jpushRegId forKey:@"jpushRegId"];
            
             NSString *getVersion =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
             [params setValue:getVersion forKey:@"appVersion"];
              NSLog(@"1-------%@",params);
            [HttpRequest requestData1:@"Common/Login" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        
                        [Global hideProgressHUDInternal:self.view];
                    NSLog(@"login-------%@",severDataDict.dataDict);
//
//                        
                        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                        
                        NSMutableArray* funConfigs = [user objectForKey:@"funConfigs"];
                        
                        bool bol = false;
                        
                        //创建俩新的数组
                        NSMutableArray *oldArr = [NSMutableArray arrayWithArray:funConfigs];
                        NSMutableArray *newArr = [NSMutableArray arrayWithArray:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"funConfigs"]];
                        
                        
                        //对数组1排序。
                        
                        [oldArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                            return obj1 > obj2;
                        }];
                        
                        [newArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                            return obj1 > obj2;
                        }];
                        
                        
                        if (newArr.count == oldArr.count) {
                            
                            bol = true;
                            for (int16_t i = 0; i < oldArr.count; i++) {
                                
                                NSDictionary *c1 = [oldArr objectAtIndex:i];
                                NSDictionary *newc = [newArr objectAtIndex:i];
                                
                                if ((newc!=c1)) {
                                    bol = true;
                                    
                                }
                            }
                        }else{
                            bol = false;
                            
                        }
                        
                        if (bol) {
                            NSLog(@"两个数组的内容相同！"); //首页的不变
                        }
                        else {
                        
                            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent0"];
                            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent1"];
                            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent2"];
                            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent3"];
                            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent4"];
                            
                            NSLog(@"两个数组的内容不相同！");   
                       }

                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"companyId" ] forKey:@"companyId"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"fid" ] forKey:@"fid"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"tokenId" ] forKey:@"tokenId"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"userCode" ] forKey:@"userCode"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"userName" ] forKey:@"userName"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"companyName" ] forKey:@"companyName"];
                        [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"userStatus" ]] forKey:@"userStatus"];
                        
                         [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"sex" ]] forKey:@"sex"];
                        [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"groupId" ]] forKey:@"groupId"];
                       
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"serverAddress"] forKey:@"serverAddress"];
                        
                        
                         [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"funConfigs"] forKey:@"funConfigs"];
                        
                        
                        
                        [user setObject:password forKey:@"passwordTextField"];
                        [user setObject:userName forKey:@"userNameTextField"];
                        [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"userType" ]  forKey:@"userType"];
                        [user synchronize];
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeTokenId" object:nil];
                       [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

                        
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];
            
        }}

}


-(void)registed:(NSString*)sender{
    
    RegistedViewController* Login=[[RegistedViewController alloc]init];
    
    [self.navigationController pushViewController:Login animated:YES];
    
    
}
- (void)forgotPassword:(NSString *)forgotPassword{

     ForgotPasswordViewController*detailViewController = [[ForgotPasswordViewController alloc] init];
     detailViewController.titleName=@"找回密码";
    [self.navigationController pushViewController:detailViewController animated:YES];


}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
